/*
 * 系统项目名称：综合决策分析系统-version2.0
 * 文件名：ShopCompanyAction.java
 * 描述：
 * 版权：Copyright 2015-2020 义乌市跨境电商供应链管理有限公司-版权所有
 * 修改人：yangjx
 * 修改时间：2016-3-7 下午3:16:05
 * 版本号：V1.1
 */
package com.ywt.cda.action;

import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.alibaba.fastjson.JSONObject;
import com.ywt.cda.access.Access;
import com.ywt.cda.action.model.ShopCompanyModel;
import com.ywt.cda.action.model.ShopCompanyOrientedModel;
import com.ywt.cda.action.vo.ShopCompanyVo;
import com.ywt.cda.core.dao.page.Page;
import com.ywt.cda.domain.Ana2ndData;
import com.ywt.cda.domain.AnaImportData;
import com.ywt.cda.domain.UserAgent;
import com.ywt.cda.domain.enums.DimensionFlag;
import com.ywt.cda.domain.enums.OrderByEnum;
import com.ywt.cda.enums.FunctionsEnum;
import com.ywt.cda.service.ParamValidateService;
import com.ywt.cda.service.ShopCompanyOrientedService;
import com.ywt.cda.service.ShopCompanyService;
import com.ywt.cda.utils.DateUtils;

/**
 * （企业定向分析） <br>
 * （功能详细描述）
 * 
 * @author yangjx
 * @version V1.0
 * @see [相关类/方法]
 * @since 2016-3-7 下午3:16:05
 */
@Controller
public class ShopCompanyOrientedAction {

	private Logger logger = Logger.getLogger(ShopCompanyOrientedAction.class);

	@Resource
	private ParamValidateService paramValidateService;
	@Resource
	private ShopCompanyService shopCompanyService;
	@Resource
	private ShopCompanyOrientedService shopCompanyOrientedService;

	@Value("#{configProperties['shop.category.amount']}")
	private String shopCategoryAmount;
	/**
	 * 出口-电商分析 getExportShopCompanyAnalyze(这里用一句话描述这个方法的作用) <br>
	 * (这里描述这个方法适用条件 – 可选)
	 * 
	 * @param shopCompanyModel
	 * @param model
	 * @return
	 * @exception
	 * @since 2016-3-11 下午1:57:55
	 * @author yangjx
	 */
	@RequestMapping("export/theshop")
	@Access(FunctionsEnum.EXPORT_THESHOP)
	public String getExportShopCompanyAnalyze(
			@ModelAttribute("shopCompanyModel") ShopCompanyModel shopCompanyModel,
			Model model, UserAgent userAgent) {
		if (null == shopCompanyModel) {

			// 默认查近七天
			shopCompanyModel = new ShopCompanyModel();
			Date date = new Date();
			String today = DateUtils.format(date, "yyyy-MM-dd");
			shopCompanyModel.setStartTime(DateUtils.getSpecifiedDayBefore(
					today, 6));
			shopCompanyModel.setEndTime(today);

			model.addAttribute("default", true);
		}
		if (null != shopCompanyModel) {
			if (StringUtils.isBlank(shopCompanyModel.getStartTime())
					&& StringUtils.isBlank(shopCompanyModel.getEndTime())) {
				// 默认查近七天

				Date date = new Date();
				String today = DateUtils.format(date, "yyyy-MM-dd");
				shopCompanyModel.setStartTime(DateUtils.getSpecifiedDayBefore(
						today, 6));
				shopCompanyModel.setEndTime(today);
				model.addAttribute("default", true);
			}
		}

		// 第一步：校验输入参数
		if (!paramValidateService.validate(shopCompanyModel)) {
			logger.info("error param!");
			model.addAttribute("error", "error param!");
			return "error";
		}

		// 第二步：进行数据库查询
		// 票数排名
		List<Ana2ndData> datas = shopCompanyService.getTopNShopData(
				shopCompanyModel, 5, OrderByEnum.SUM_NUM);

		List<ShopCompanyVo> vos = shopCompanyOrientedService.toVos(datas);

		/*
		 * // 5家企业的折线图-按票数
		 * 
		 * List<AnaImportData> shopDatas = shopCompanyOrientedService.getShopData( shopCompanyModel, vos);
		 * 
		 * JSONObject objectZhexian = new JSONObject();
		 * 
		 * shopCompanyOrientedService.toZhexianJson(objectZhexian, shopDatas, shopCompanyModel, OrderByEnum.SUM_NUM); if (null != vos && vos.size() > 0) { // 第一家企业的饼图、地图、粘合度图 List<Ana2ndData> pieDatas
		 * = shopCompanyOrientedService.getDatas( shopCompanyModel, vos, DimensionFlag.S_CATEGORY);
		 * 
		 * List<Ana2ndData> mapDatas = shopCompanyOrientedService.getDatas( shopCompanyModel, vos, DimensionFlag.PROVINCE_TYPE);
		 * 
		 * // List<Ana2ndData> nianDatas=shopCompanyOrientedService.getDatas(shopCompanyModel,vos,DimensionFlag.S_CATEGORY); JSONObject objectOtherGraph = new JSONObject();
		 * shopCompanyOrientedService.toOtherGraphJson(objectOtherGraph, pieDatas, mapDatas, null); }
		 * 
		 * // 第三步：组装数据 JSONArray result = new JSONArray(); JSONArray result1 = new JSONArray(); JSONArray result2 = new JSONArray(); JSONObject result3 = new JSONObject(); JSONArray result4 = new
		 * JSONArray(); shopCompanyService .toJsonArrayImport(datas, result, OrderByEnum.SUM_NUM);
		 * 
		 * logger.info(result.toJSONString()); logger.info(result4.toJSONString()); model.addAttribute("numarray", result.toJSONString()); model.addAttribute("pricearray", result1.toJSONString());
		 * model.addAttribute("pkgarray", result2.toJSONString()); model.addAttribute("avgobj", result3.toJSONString()); model.addAttribute("skuarray", result4.toJSONString());
		 */

		model.addAttribute("yes", shopCompanyModel.getSelectButton());
		model.addAttribute("vos", vos);
		return "export/theshop";
	}

	@RequestMapping("import/getcompanydata")
	public void getJson(ShopCompanyOrientedModel shopCompanyOrientedModel,
			PrintWriter out) {
		JSONObject object = new JSONObject();
		if (null == shopCompanyOrientedModel) {

			// 默认查近七天
			shopCompanyOrientedModel = new ShopCompanyOrientedModel();
			Date date = new Date();
			String today = DateUtils.format(date, "yyyy-MM-dd");
			shopCompanyOrientedModel.setStartTime(DateUtils
					.getSpecifiedDayBefore(today, 6));
			shopCompanyOrientedModel.setEndTime(today);

		}
		if (null != shopCompanyOrientedModel) {
			if (StringUtils.isBlank(shopCompanyOrientedModel.getStartTime())
					&& StringUtils.isBlank(shopCompanyOrientedModel
							.getEndTime())) {
				// 默认查近七天

				Date date = new Date();
				String today = DateUtils.format(date, "yyyy-MM-dd");
				shopCompanyOrientedModel.setStartTime(DateUtils
						.getSpecifiedDayBefore(today, 6));
				shopCompanyOrientedModel.setEndTime(today);

			}
		}
		// 第一步：校验输入参数
		if (!paramValidateService.validate(shopCompanyOrientedModel)) {
			logger.info("error param!");
			object.put("success", false);
			object.put("message", "参数错误");
			out.print(object.toJSONString());
			return;
		}
		shopCompanyOrientedService.updateModel(shopCompanyOrientedModel);
		
		List<ShopCompanyVo> vos = shopCompanyOrientedService
				.toVo(shopCompanyOrientedModel);
		if(null!=vos&&vos.size()==0){
			object.put("success", false);
			object.put("message", "请至少选择一家企业！");
			out.print(object.toJSONString());
			return;
		}
		// 5家企业的折线图-按票数

		List<AnaImportData> shopDatas = shopCompanyOrientedService.getShopData(
				shopCompanyOrientedModel, vos);

		JSONObject objectZhexian = new JSONObject();

		shopCompanyOrientedService.toZhexianJson(objectZhexian, shopDatas,
				shopCompanyOrientedModel,vos,
				null != shopCompanyOrientedModel.getOrderByEnum()
						? shopCompanyOrientedModel.getOrderByEnum()
							: OrderByEnum.SUM_NUM);
		if (null != vos && vos.size() > 0) {
			// 第一家企业的饼图、地图、粘合度图
			List<Ana2ndData> pieDatas = shopCompanyOrientedService.getDatas(
					shopCompanyOrientedModel, vos, DimensionFlag.S_CATEGORY,new Page(1,Integer.valueOf(shopCategoryAmount)));

			List<Ana2ndData> mapDatas = shopCompanyOrientedService.getDatas(
					shopCompanyOrientedModel, vos, DimensionFlag.PROVINCE_TYPE,null);

			 List<Ana2ndData> nianDatas=shopCompanyOrientedService.getNianDatas(shopCompanyOrientedModel,vos,DimensionFlag.NIAN_TYPE);
			JSONObject objectOtherGraph = new JSONObject();
			shopCompanyOrientedService.toOtherGraphJson(objectOtherGraph,
					pieDatas, mapDatas, nianDatas,vos);
			object.put("objectOtherGraph", objectOtherGraph);
		}
		object.put("success", true);
		object.put("objectZhexian", objectZhexian);
		out.print(object.toJSONString());
	}
	@RequestMapping("export/getcompanydata")
	public void getExportJson(ShopCompanyOrientedModel shopCompanyOrientedModel,
			PrintWriter out) {
		JSONObject object = new JSONObject();
		if (null == shopCompanyOrientedModel) {

			// 默认查近七天
			shopCompanyOrientedModel = new ShopCompanyOrientedModel();
			Date date = new Date();
			String today = DateUtils.format(date, "yyyy-MM-dd");
			shopCompanyOrientedModel.setStartTime(DateUtils
					.getSpecifiedDayBefore(today, 6));
			shopCompanyOrientedModel.setEndTime(today);

		}
		if (null != shopCompanyOrientedModel) {
			if (StringUtils.isBlank(shopCompanyOrientedModel.getStartTime())
					&& StringUtils.isBlank(shopCompanyOrientedModel
							.getEndTime())) {
				// 默认查近七天

				Date date = new Date();
				String today = DateUtils.format(date, "yyyy-MM-dd");
				shopCompanyOrientedModel.setStartTime(DateUtils
						.getSpecifiedDayBefore(today, 6));
				shopCompanyOrientedModel.setEndTime(today);

			}
		}
		// 第一步：校验输入参数
		if (!paramValidateService.validate(shopCompanyOrientedModel)) {
			logger.info("error param!");
			object.put("success", false);
			object.put("message", "参数错误");
			out.print(object.toJSONString());
			return;
		}
		shopCompanyOrientedService.updateModel(shopCompanyOrientedModel);
		
		List<ShopCompanyVo> vos = shopCompanyOrientedService
				.toVo(shopCompanyOrientedModel);

		// 5家企业的折线图-按票数
		if(null!=vos&&vos.size()==0){
			object.put("success", false);
			object.put("message", "请至少选择一家企业！");
			out.print(object.toJSONString());
			return;
		}
		List<Ana2ndData> shopDatas = shopCompanyOrientedService.getExportShopData(
				shopCompanyOrientedModel, vos);

		JSONObject objectZhexian = new JSONObject();

		shopCompanyOrientedService.toExportZhexianJson(objectZhexian, shopDatas,
				shopCompanyOrientedModel,vos,
				null != shopCompanyOrientedModel.getOrderByEnum()
						? shopCompanyOrientedModel.getOrderByEnum()
							: OrderByEnum.SUM_NUM);
		if (null != vos && vos.size() > 0) {
			// 第一家企业的饼图、地图、粘合度图
			List<Ana2ndData> pieDatas = shopCompanyOrientedService.getExportDatas(
					shopCompanyOrientedModel, vos, DimensionFlag.S_CATEGORY,new Page(1,Integer.valueOf(shopCategoryAmount)));

			

			// List<Ana2ndData> nianDatas=shopCompanyOrientedService.getDatas(shopCompanyModel,vos,DimensionFlag.S_CATEGORY);
			
			JSONObject objectOtherGraph = new JSONObject();
			shopCompanyOrientedService.toOtherGraphJson(objectOtherGraph,
					pieDatas, null, null,vos);
			object.put("objectOtherGraph", objectOtherGraph);
		}
		object.put("success", true);
		object.put("objectZhexian", objectZhexian);
		out.print(object.toJSONString());
	}
	@RequestMapping("import/theshop")
	@Access(FunctionsEnum.IMPORT_THESHOP)
	public String getImportShopCompanyAnalyze(
			@ModelAttribute("shopCompanyModel") ShopCompanyModel shopCompanyModel,
			Model model, UserAgent userAgent) {
		if (null == shopCompanyModel) {

			// 默认查近七天
			shopCompanyModel = new ShopCompanyModel();
			Date date = new Date();
			String today = DateUtils.format(date, "yyyy-MM-dd");
			shopCompanyModel.setStartTime(DateUtils.getSpecifiedDayBefore(
					today, 6));
			shopCompanyModel.setEndTime(today);

			model.addAttribute("default", true);
		}
		if (null != shopCompanyModel) {
			if (StringUtils.isBlank(shopCompanyModel.getStartTime())
					&& StringUtils.isBlank(shopCompanyModel.getEndTime())) {
				// 默认查近七天

				Date date = new Date();
				String today = DateUtils.format(date, "yyyy-MM-dd");
				shopCompanyModel.setStartTime(DateUtils.getSpecifiedDayBefore(
						today, 6));
				shopCompanyModel.setEndTime(today);
				model.addAttribute("default", true);
			}
		}

		// 第一步：校验输入参数
		if (!paramValidateService.validate(shopCompanyModel)) {
			logger.info("error param!");
			model.addAttribute("error", "error param!");
			return "error";
		}

		// 第二步：进行数据库查询
		// 票数排名
		List<AnaImportData> datas = shopCompanyService.getTopNShopImportData(
				shopCompanyModel, 5, OrderByEnum.SUM_NUM);

		List<ShopCompanyVo> vos = shopCompanyOrientedService.toVo(datas);

		/*
		 * // 5家企业的折线图-按票数
		 * 
		 * List<AnaImportData> shopDatas = shopCompanyOrientedService.getShopData( shopCompanyModel, vos);
		 * 
		 * JSONObject objectZhexian = new JSONObject();
		 * 
		 * shopCompanyOrientedService.toZhexianJson(objectZhexian, shopDatas, shopCompanyModel, OrderByEnum.SUM_NUM); if (null != vos && vos.size() > 0) { // 第一家企业的饼图、地图、粘合度图 List<Ana2ndData> pieDatas
		 * = shopCompanyOrientedService.getDatas( shopCompanyModel, vos, DimensionFlag.S_CATEGORY);
		 * 
		 * List<Ana2ndData> mapDatas = shopCompanyOrientedService.getDatas( shopCompanyModel, vos, DimensionFlag.PROVINCE_TYPE);
		 * 
		 * // List<Ana2ndData> nianDatas=shopCompanyOrientedService.getDatas(shopCompanyModel,vos,DimensionFlag.S_CATEGORY); JSONObject objectOtherGraph = new JSONObject();
		 * shopCompanyOrientedService.toOtherGraphJson(objectOtherGraph, pieDatas, mapDatas, null); }
		 * 
		 * // 第三步：组装数据 JSONArray result = new JSONArray(); JSONArray result1 = new JSONArray(); JSONArray result2 = new JSONArray(); JSONObject result3 = new JSONObject(); JSONArray result4 = new
		 * JSONArray(); shopCompanyService .toJsonArrayImport(datas, result, OrderByEnum.SUM_NUM);
		 * 
		 * logger.info(result.toJSONString()); logger.info(result4.toJSONString()); model.addAttribute("numarray", result.toJSONString()); model.addAttribute("pricearray", result1.toJSONString());
		 * model.addAttribute("pkgarray", result2.toJSONString()); model.addAttribute("avgobj", result3.toJSONString()); model.addAttribute("skuarray", result4.toJSONString());
		 */

		model.addAttribute("yes", shopCompanyModel.getSelectButton());
		model.addAttribute("vos", vos);
		return "import/theshop";
	}

	@RequestMapping("/getshops")
	public void getShops(@RequestParam("keyword") String keyword,
			PrintWriter out) {
		JSONObject jsonObject = new JSONObject();

		shopCompanyOrientedService.setJsonShops(jsonObject, keyword);
		out.print(jsonObject.toJSONString());

	}

}
